package cl.foursoft.sirvicod.service;

import java.sql.Connection;
import java.util.List;

import cl.foursoft.sirvicod.dao.facade.FacadeDepartamento;
import cl.foursoft.sirvicod.dao.facade.FacadeObservacion;
import cl.foursoft.sirvicod.dao.implementacion.DBMannager;
import cl.foursoft.sirvicod.dao.transferObject.ObservacionTO;
import cl.foursoft.sirvicod.util.UtilLog;

public class ObservacionService {
	
	public List<ObservacionTO> obtenerObservacionesActivas(){
		Connection c = DBMannager.getConnection();
		FacadeObservacion facadeObservacion = new FacadeObservacion();
		List<ObservacionTO> resp = null;
		
		try {			
			resp = facadeObservacion.obtenerObservacionesActivas(c);
			
			boolean exito = (resp != null);		
			DBMannager.close(c, exito);
		}
		catch (Exception ex) 
		{
			UtilLog.registrar(ex);
			DBMannager.closeOnError(c);
		}
		
		return resp;
	}
	
	public List<ObservacionTO> obtenerObservacionesActivasPorDepto(int depto){
		Connection c = DBMannager.getConnection();
		FacadeObservacion facadeObservacion = new FacadeObservacion();
		List<ObservacionTO> resp = null;
		
		try {			
			resp = facadeObservacion.obtenerObservacionesActivasPorDepto(depto,c);
			
			boolean exito = (resp != null);		
			DBMannager.close(c, exito);
		}
		catch (Exception ex) 
		{
			UtilLog.registrar(ex);
			DBMannager.closeOnError(c);
		}
		
		return resp;
	}
	
	public int guardarObservacion(ObservacionTO observacion){
		Connection c = DBMannager.getConnection();
		FacadeObservacion facadeObservacion = new FacadeObservacion();
		FacadeDepartamento facadeDepto = new FacadeDepartamento();
		int resp = -1;
		
		try {		
			if(facadeDepto.existeDepartamento(observacion.getNumeroDepto(), c))
				resp = facadeObservacion.guardarObservacion(observacion, c);
			else
				resp = -2;
			
			boolean exito = (resp > -1);		
			DBMannager.close(c, exito);
		}
		catch (Exception ex) 
		{
			UtilLog.registrar(ex);
			DBMannager.closeOnError(c);
		}
		
		return resp;
	}
	
	public int eliminarObservacion(int idObservacion){
		Connection c = DBMannager.getConnection();
		FacadeObservacion facadeObservacion = new FacadeObservacion();
		int resp = -1;
		
		try {		
			resp = facadeObservacion.eliminarObservacion(idObservacion, c);
			
			boolean exito = (resp > -1);		
			DBMannager.close(c, exito);
		}
		catch (Exception ex) 
		{
			UtilLog.registrar(ex);
			DBMannager.closeOnError(c);
		}
		
		return resp;
	}
	
}
